<?php
/* -------------------------------------------------------------------------------------
* 	ID:						productsshipping.php
* 	Letzter Stand:			3
* 	zuletzt geaendert von:	siekiera
* 	Datum:					18.04.2011
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class productsshipping {
	var $code, $title, $description, $enabled, $num_zones, $db;

    function __construct() {
    	global $db;

		$this->code = 'productsshipping';
		$this->title = MODULE_SHIPPING_PRODUCTSSHIPPING_TEXT_TITLE;
		$this->description = MODULE_SHIPPING_PRODUCTSSHIPPING_TEXT_DESCRIPTION;
		$this->sort_order = MODULE_SHIPPING_PRODUCTSSHIPPING_SORT_ORDER;
		$this->icon = '';
		$this->tax_class = MODULE_SHIPPING_PRODUCTSSHIPPING_TAX_CLASS;
		$this->enabled = ((MODULE_SHIPPING_PRODUCTSSHIPPING_STATUS == 'True') ? true : false);
		$this->db = $db;
		$this->num_zones = 10;
    }

	function get_product_shipping_costs() {
		global $price, $order;

		$products = $_SESSION['cart']->get_products();
		$costs = 0;
		$infos = array();

		for($i=0; $i < sizeof($products); $i++) {
			$result = '';
			$result = $this->db->db_query("SELECT
												products_shipping_costs AS costs
											FROM
												".TABLE_PRODUCTS."
											WHERE
												products_shipping_costs !=''
											AND
												products_id = '". get_prid($products[$i]['id'])."'");

			if($result->_numOfRows) {
				while(!$result->EOF) {
					$p_cost = '';
					if(strstr($row->fields['costs'], ':')) {
						$tmp = explode(';', $result->fields['costs']);
						for($c=0; $c < sizeof($tmp); $c++) {
							$z = explode(':', $tmp[$c]);
							if($z[0] == $order->delivery['country']['iso_code_2']) {
								$new_price = '+'.$price->format($z[1] * $products[$i]['quantity'], true, $this->tax_class, true).' f&uuml;r '.$z[0];
								$costs += ($z[1] * $products[$i]['quantity']);
								$infos[] = array('title' => $products[$i]['quantity'] .'x '. $products[$i]['name'],
												'price' => $new_price);
								break;
							}
						}

					} elseif (!empty($result->fields['costs']) && $result->fields['costs']>0) {
						$new_price = '+'.$price->format($result->fields['costs'] * $products[$i]['quantity'], true, $this->tax_class, true);
						$costs += ($result->fields['costs'] * $products[$i]['quantity']);
						$infos[] = array('title' => $products[$i]['quantity'] .'x '. $products[$i]['name'],
										'price' => $new_price);
					}
					$result->MoveNext();
				}
			}
		}
		$output = array('costs' => $costs, 'infos' => $infos);

		return $output;
	}

    function quote($method = '') {
    	if(!free_shipping())
			return;
    	
		global $order,$shipping_weight,$price;

		$dest_country = $order->delivery['country']['iso_code_2'];
		$dest_zone = 0;
		$error = false;

		for($i=1; $i <= $this->num_zones; $i++) {
			$countries_table = constant('MODULE_SHIPPING_PRODUCTSSHIPPING_COUNTRIES_'.$i);
			$country_zones = explode(",", $countries_table);
			if(in_array($dest_country, $country_zones)) {
				$dest_zone = $i;
				break;
			}
		}

		if($dest_zone == 0)
			$error = true;
		else {
			$products_data = $this->get_product_shipping_costs();

			$products_info = '';

			$shipping = -1;
			$zones_cost = constant('MODULE_SHIPPING_PRODUCTSSHIPPING_COST_'.$dest_zone);

			$zones_table = explode(",", $zones_cost);
			$size = sizeof($zones_table);
			for ($i=0; $i<$size; $i+=2) {
				$zone_weight = explode(':', $zones_table[$i]);
				if ($shipping_weight <= $zone_weight[0]) {
					$shipping = $zone_weight[1];
					$shipping_method = MODULE_SHIPPING_PRODUCTSSHIPPING_TEXT_WAY.' '.$dest_country .' ';

					$products_info .= '<span class="ot_shipping_productsshipping">';
					foreach ($products_data['infos'] as $info)
						$products_info .= '<br /><em class="fs85">'.$info['title'] .': '. $info['price'].'</em>';

					$products_info .= '</span>';
					break;
				}
			}

			if ($shipping == -1) {
				$shipping_cost = 0;
				$shipping_method = MODULE_SHIPPING_PRODUCTSSHIPPING_UNDEFINED_RATE;

			} else {
				$zones_cost_full = $shipping + constant('MODULE_SHIPPING_PRODUCTSSHIPPING_HANDLING_'.$dest_zone);
				$shipping_cost = $zones_cost_full + $products_data['costs'];
				if($zones_cost_full > 0)
					$shipping_method .= $price->format($zones_cost_full, true, $this->tax_class, true).$products_info;
				else
					$shipping_method .= $products_info;
			}
		}

      $this->quotes = array('id' => $this->code,
                            'module' => MODULE_SHIPPING_PRODUCTSSHIPPING_TEXT_TITLE,
                            'methods' => array(array('id' => $this->code,
                                                     'title' => $shipping_method,
                                                     'cost' => $shipping_cost)));

		if($this->tax_class > 0)
			$this->quotes['tax'] = get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);

		if(not_null($this->icon))
			$this->quotes['icon'] = image($this->icon, $this->title);

		if($error == true)
			$this->quotes['error'] = MODULE_SHIPPING_PRODUCTSSHIPPING_INVALID_ZONE;

		return $this->quotes;
    }

	function check() {
		if (!isset($this->_check)) {
			$check = $this->db->db_query("SELECT
												configuration_value
											FROM
												".TABLE_CONFIGURATION."
											WHERE
												configuration_key = 'MODULE_SHIPPING_PRODUCTSSHIPPING_STATUS'");
			$this->_check = $check->_numOfRows;
		}
		return $this->_check;
	}

    function install() {
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_SHIPPING_PRODUCTSSHIPPING_STATUS', 'True', '6', '0', 'cfg_select_option(array(\'True\', \'False\'), ', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_PRODUCTSSHIPPING_ALLOWED', '', '6', '0', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_SHIPPING_PRODUCTSSHIPPING_TAX_CLASS', '0', '6', '0', 'get_tax_class_title', 'cfg_pull_down_tax_classes(', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_PRODUCTSSHIPPING_SORT_ORDER', '0', '6', '0', now())");
		for($i = 1; $i <= $this->num_zones; $i++) {
			$default_countries = '';
			if ($i == 1)
				$default_countries = 'DE,AT';
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_PRODUCTSSHIPPING_COUNTRIES_".$i ."', '".$default_countries."', '6', '0', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_PRODUCTSSHIPPING_COST_".$i ."', '3:8.50,7:10.50,99:20.00', '6', '0', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_PRODUCTSSHIPPING_HANDLING_".$i."', '0', '6', '0', now())");
		}
    }

    function remove() {
		$this->db->db_query("DELETE FROM ".TABLE_CONFIGURATION." WHERE configuration_key IN ('".implode("', '", $this->keys())."')");
    }

    function keys() {
		$keys = array('MODULE_SHIPPING_PRODUCTSSHIPPING_STATUS','MODULE_SHIPPING_PRODUCTSSHIPPING_ALLOWED', 'MODULE_SHIPPING_PRODUCTSSHIPPING_TAX_CLASS', 'MODULE_SHIPPING_PRODUCTSSHIPPING_SORT_ORDER');

		for ($i=1; $i<=$this->num_zones; $i++) {
			$keys[] = 'MODULE_SHIPPING_PRODUCTSSHIPPING_COUNTRIES_'.$i;
			$keys[] = 'MODULE_SHIPPING_PRODUCTSSHIPPING_COST_'.$i;
			$keys[] = 'MODULE_SHIPPING_PRODUCTSSHIPPING_HANDLING_'.$i;
		}
		return $keys;
    }
}
?>